package com.topvision.controller.ar;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.topvision.init.InitProperties;
import com.topvision.util.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topvision.model.ar.FaceGroup;
import com.topvision.service.ar.FaceGroupService;
import com.topvision.controller.AbstractController;

/**
 * 人脸识别组
 *
 * @author shengwm
 * @email 179653076@qq.com
 * @date 2017-10-10 16:12:34
 */
@RestController
@RequestMapping("ar/facegroup")
public class FaceGroupController extends AbstractController {
    @Autowired
    private FaceGroupService faceGroupService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("ar/facegroup:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<FaceGroup> faceGroupList = faceGroupService.queryList(query);
        int total = faceGroupService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(faceGroupList, total, query.getLimit(), query.getPage());

        return R.success().put("page", pageUtil);
    }

    @RequestMapping("/findListByParam")
    @RequiresPermissions("ar/facegroup:list")
    public R findListByParam(@RequestParam Map<String, Object> params) {

        List<FaceGroup> faceGroupList = faceGroupService.queryByParam(params);

        return R.success().put("faceGroupList", faceGroupList);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("ar/facegroup:info")
    public R info(@PathVariable("id") Long id) {
        FaceGroup faceGroup = faceGroupService.queryObject(id);
        return R.success().put("faceGroup", faceGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("ar/facegroup:save")
    public R save(@RequestBody FaceGroup faceGroup) {
        JSONObject jsonObject = FaceApiUtil.createGroup(faceGroup.getCode(), faceGroup.getName());
        String resultcode = jsonObject.get("result_code").toString();
        logger.info(resultcode);
        if (Integer.valueOf(jsonObject.get("result_code").toString()) == 0) {
            faceGroupService.save(faceGroup);
            return R.success();
        } else {
            return R.error().setMsg("人脸服务器开小差啦...");
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("ar/facegroup:update")
    public R update(@RequestBody FaceGroup faceGroup) {
        faceGroupService.update(faceGroup);
        return R.success();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("ar/facegroup:delete")
    public R delete(@RequestBody Long[] ids) {
        FaceGroup faceGroup = faceGroupService.queryObject(ids[0]);
        JSONObject jsonObject = FaceApiUtil.delGroup(faceGroup.getCode());
        String resultcode = jsonObject.get("result_code").toString();
        logger.info(resultcode);
        if (Integer.valueOf(jsonObject.get("result_code").toString()) == 0) {
            faceGroupService.deleteBatch(ids);
            return R.success();
        } else {
            return R.error().setMsg("人脸服务器开小差啦...");
        }
    }

}
